//
//  ByHomeVC.m
//  recordworktool_ios
//
//  Created by mc on 2017/11/30.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ByHomeVC.h"
#import "ByRecordVC.h"
#import "ByHomeHeadView.h"


#import "ByRecordListVC.h"
#import "ByAccountDetailVC.h"
#import "BSLCalendar.h"
#import "WeeksView.h"
#import "ByMonthDatePicker.h"
#import "BSLMonthCollectionView.h"
#import "CalendarModel.h"
#import "JSONKit.h"

@interface ByHomeVC ()

@property (nonatomic, strong) ZKBombView *monthDateView;
@property (nonatomic, assign) NSInteger length;
@property (nonatomic, strong) BSLCalendar *calendar;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) ByHomeHeadView *headView;

@end

@implementation ByHomeVC

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (ZKBombView *)monthDateView{
    if (_monthDateView == nil) {
        _monthDateView = [[ZKBombView alloc] init];
        _monthDateView.popMode = ZKBombPopModeBottom;
        _monthDateView.positionMode = ZKBombPositionModeBottom;
        _monthDateView.usePickerTitleForCustomContentMode = YES;
        
        ByMonthDatePicker *pick = [[ByMonthDatePicker alloc] init];
        pick.frame = CGRectMake(0, 0, SCREEN_WIDTH, 240);
        _monthDateView.contentView = pick;
        
        _monthDateView.removeBlock = ^(){
            NSLog(@"取消");
        };
        pick.dateDidPicker = ^(NSArray *dateArray){
            [self getLengthWithYear: [[dateArray firstObject] integerValue] withMonth:[[dateArray lastObject] integerValue]];
            
        };
        __weak typeof(self) weakSelf = self;
        _monthDateView.customTapOk = ^(){
             NSLog(@"确定");
            [weakSelf.calendar.dayView calendarContainerWhereMonth:weakSelf.length month:^(MonthModel *month) {
                weakSelf.calendar.weeks.month = month.month;
            }];
            
        };
       
        
    }
    return _monthDateView;
}

- (void)getLengthWithYear:(NSInteger)year withMonth:(NSInteger)month{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    NSInteger currentYear = components.year;
    NSInteger currentMonth = components.month;
    NSInteger length = 0;
    if (year == currentYear) {
        length = month - currentMonth;
    }else{
        length = (year - currentYear) * 12 + month - currentMonth;
    }
    self.length = length;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.length = 0;
    [self initViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
    [self loadData];
    
    [self loadTodayData];
}

- (void)loadData{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.uid = BY_USER.uid;
    netModel.mobile = BY_USER.mobile;
    
    if (BY_USER.isWorker.integerValue == 1) {
        
        [ByApiHome recordWorkerIndex:netModel success:^(ByNetBaseResult *result){
            NSArray *array = result.data;
            NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
            for (int i = 0; i < array.count; i++){
                NSDictionary *dic = [array objectAtIndex:i];
                NSString *day = [NSString stringWithFormat:@"%@",dic[@"day"]];
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
                if (day.length == 8) {
                    NSString *year = [day substringWithRange:NSMakeRange(0, 4)];
                    NSString *month = [day substringWithRange:NSMakeRange(4, 2)];
                    NSString *dayStr = [day substringWithRange:NSMakeRange(6, 2)];
                    NSLog(@"%@ %@ %@", year,month,dayStr);
                    netModel.year = year;
                    netModel.month = [NSString stringWithFormat:@"%ld", month.integerValue];
                    netModel.day = [NSString stringWithFormat:@"%ld", dayStr.integerValue];
                }
                [tempArray addObject:netModel];
            }
            if (tempArray.count > 0) {
                self.calendar.dataArray = tempArray;
            }
        } failure:^(ByNetBaseResult *result){
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }else{
        
        [ByApiHome recordOverseerIndex:netModel success:^(ByNetBaseResult *result) {
            
            NSArray *array = result.data;
            NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
            for (int i = 0; i < array.count; i++){
                NSDictionary *dic = [array objectAtIndex:i];
                NSString *day = [NSString stringWithFormat:@"%@",dic[@"day"]];
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
                if (day.length == 8) {
                    NSString *year = [day substringWithRange:NSMakeRange(0, 4)];
                    NSString *month = [day substringWithRange:NSMakeRange(4, 2)];
                    NSString *dayStr = [day substringWithRange:NSMakeRange(6, 2)];
                    NSLog(@"%@ %@ %@", year,month,dayStr);
                    netModel.year = year;
                    netModel.month = [NSString stringWithFormat:@"%ld", month.integerValue];
                    netModel.day = [NSString stringWithFormat:@"%ld", dayStr.integerValue];
                }
                [tempArray addObject:netModel];
            }
            if (tempArray.count > 0) {
                self.calendar.dataArray = tempArray;
            }
            
            
        } failure:^(ByNetBaseResult *result){
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }
   
   
   
}

- (void)loadTodayData{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    NSInteger year =  components.year;
    NSInteger month = components.month;
    NSInteger day = components.day;
    NSString *monthStr = @"";
    NSString *dayStr = @"";
    if (month < 10) {
        monthStr = [NSString stringWithFormat:@"0%ld", month];
    }else{
        monthStr = [NSString stringWithFormat:@"%ld", month];
    }
    
    if (day < 10) {
        dayStr = [NSString stringWithFormat:@"0%ld", day];
    }else{
        dayStr = [NSString stringWithFormat:@"%ld", day];
    }
    
    
    NSString *timeString = [NSString stringWithFormat:@"%ld%@%@", year,monthStr,dayStr];
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.uid = BY_USER.uid;
    netModel.mobile = BY_USER.mobile;
    netModel.daytime = timeString;
    
    if (BY_USER.isWorker.integerValue == 1) {
        
        [ByApiRecord queryWorker:netModel success:^(ByNetBaseResult *result){
            
            [ByCoreToolAlert hideHud];
            NSArray *dataArray = result.data;
            self.dataArray = dataArray;
            if (dataArray.count == 0) {
                self.headView.stateLabel.text = @"未记";
                self.headView.stateLabel.textColor = [UIColor redColor];
            }else{
                
                ByNetModel *netModel = [self.dataArray firstObject];
                NSMutableDictionary *dic;
                NSString *normal_work_time;
                NSString *over_work_time ;
                self.headView.stateLabel.text = @"已记";
                self.headView.stateLabel.textColor = [UIColor whiteColor];
                if (netModel.audit_status.integerValue == 0) {
                    self.headView.stateLabel.text = @"待审查";
                    self.headView.stateLabel.textColor = [UIColor redColor];
                }else if (netModel.audit_status.integerValue == 1){
                    self.headView.stateLabel.text = @"已审阅";
                    self.headView.stateLabel.textColor = [UIColor whiteColor];
                }else if (netModel.audit_status.integerValue == 2){
                    self.headView.stateLabel.text = @"存疑";
                    self.headView.stateLabel.textColor = [UIColor redColor];
                }
                if (netModel.extra && netModel.extra.length > 0 && ![netModel.extra isEqualToString:@"[]"]) {
                    dic = [netModel.extra mutableObjectFromJSONString];
                    normal_work_time = dic[@"normal_worktime"];
                    over_work_time = dic[@"over_worktime"];
                    
                    self.headView.leftView.valueLabel.text = normal_work_time;
                    self.headView.rightView.valueLabel.text = over_work_time;
                    self.headView.centerView.valueLabel.text = [NSString stringWithFormat:@"%.1f",netModel.money.floatValue];
                }
            }
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }else{
        
        [ByApiRecord queryOverseer:netModel success:^(ByNetBaseResult *result) {
            
            [ByCoreToolAlert hideHud];
            NSArray *dataArray = result.data;
            self.dataArray = dataArray;
            CGFloat money = 0;
            for (int i = 0; i < self.dataArray.count; i++) {
                ByNetModel *netModel = [self.dataArray objectAtIndex:i];
                if (netModel.money) {
                    money = money + netModel.money.floatValue;
                }
                
            }
            self.headView.rightView.valueLabel.text = [NSString stringWithFormat:@"%.1f",money];
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }
    
}


- (void)initViews{
    CGFloat height = 0;
    if (Device_Is_iPhoneX) {
        height = 280 + 88 - 64;
    }else{
        height = 280;
    }
    
    CGFloat H = 778 - 34 - 22 - 49;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -STATUS_NAV, SCREEN_WIDTH, SCREEN_HEIGHT - 49 + STATUS_NAV)];
    [self.view addSubview:scrollView];
    if (Device_Is_iPhoneX) {
        H = 778 - 49;
        scrollView.contentSize = CGSizeMake(scrollView.size.width, H);
    }else{
        scrollView.contentSize = CGSizeMake(scrollView.size.width, H);
    }
    
    
    ByHomeHeadView *headView = [[ByHomeHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    [scrollView addSubview:headView];
    headView.recorded = ^{
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
        NSInteger year =  components.year;
        NSInteger month = components.month;
        NSInteger day = components.day;
        NSString *monthStr = @"";
        NSString *dayStr = @"";
        if (month < 10) {
            monthStr = [NSString stringWithFormat:@"0%ld", month];
        }else{
            monthStr = [NSString stringWithFormat:@"%ld", month];
        }
        
        if (day < 10) {
            dayStr = [NSString stringWithFormat:@"0%ld", day];
        }else{
            dayStr = [NSString stringWithFormat:@"%ld", day];
        }
    
        NSString *timeString = [NSString stringWithFormat:@"%ld%@%@", year,monthStr,dayStr];
        if (BY_USER.isWorker.integerValue == 1) {
            if (self.dataArray.count == 0) {
                ByRecordVC *vc = [[ByRecordVC alloc] init];
                vc.isWorker = BY_USER.isWorker.integerValue == 1;
                [self by_normalPush:vc hasNavi:YES];
                
            }else{
                ByRecordVC *vc = [[ByRecordVC alloc] init];
                vc.isWorker = BY_USER.isWorker.integerValue == 1;
                vc.netModel = [self.dataArray firstObject];
                vc.isUpdate = YES;
                [self by_normalPush:vc hasNavi:YES];
            }
        }else{
            
            ByRecordListVC *vc = [[ByRecordListVC alloc] init];
            vc.daytime = timeString;
            [self by_normalPush:vc hasNavi:YES];
        }
      
    };
    self.headView = headView;
    
    BSLCalendar *calendar = [[BSLCalendar alloc]initWithFrame:CGRectMake(0, height, SCREEN_WIDTH, H - height - 40)];
    [scrollView addSubview:calendar];
    [calendar.weeks.selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    calendar.showChineseCalendar = NO;
    [calendar selectDateOfMonth:^(NSInteger year, NSInteger month, NSInteger day) {
        NSLog(@"%ld年/%ld月/%ld日",(long)year,(long)month,(long)day);
        NSString *monthStr = @"";
        NSString *dayStr = @"";
        if (month < 10) {
            monthStr = [NSString stringWithFormat:@"0%ld", month];
        }else{
            monthStr = [NSString stringWithFormat:@"%ld", month];
        }
        
        if (day < 10) {
            dayStr = [NSString stringWithFormat:@"0%ld", day];
        }else{
            dayStr = [NSString stringWithFormat:@"%ld", day];
        }
        
        NSString *timeString = [NSString stringWithFormat:@"%ld%@%@", year,monthStr,dayStr];
        ByRecordListVC *vc = [[ByRecordListVC alloc] init];
        vc.daytime = timeString;
        [self by_normalPush:vc hasNavi:YES];
        
    }];
    self.calendar = calendar;
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, H - 40, SCREEN_WIDTH, 40)];
    [scrollView addSubview:bottomView];
    
    UILabel *line = [[UILabel alloc] init];
    [bottomView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomView).offset(0);
        make.right.equalTo(bottomView).offset(0);
        make.top.equalTo(bottomView).offset(0);
        make.height.mas_equalTo(@1);
    }];
    line.backgroundColor = BY_LINE_COLOR;
    
    
    UILabel *personLabel = [[UILabel alloc] init];
    [bottomView addSubview:personLabel];
    [personLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomView).offset(30);
        make.top.equalTo(bottomView).offset(5);
        make.size.mas_equalTo(CGSizeMake(120, 20));
    }];
    personLabel.textColor = BY_THEME_COLOR;
    personLabel.font = FONT(12);
    personLabel.text = @"1p=1个工 1h=1小时";
    
    UILabel *rightLabel = [[UILabel alloc] init];
    [bottomView addSubview:rightLabel];
    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bottomView).offset(-30);
        make.top.equalTo(personLabel.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(30, 20));
    }];
    rightLabel.font = FONT(12);
    rightLabel.textColor = [UIColor blackColor];
    rightLabel.text = @"加班";
    
    UIView *blueView = [[UIView alloc] init];
    [bottomView addSubview:blueView];
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(rightLabel.mas_left).offset(-5);
        make.centerY.equalTo(rightLabel.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    blueView.backgroundColor = BY_THEME_COLOR;
    
    UILabel *workLabel = [[UILabel alloc] init];
    [bottomView addSubview:workLabel];
    [workLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(blueView.mas_left).offset(-5);
        make.top.equalTo(rightLabel.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(30, 20));
    }];
    workLabel.font = FONT(12);
    workLabel.textColor = [UIColor blackColor];
    workLabel.text = @"上班";
    
    UIView *redView = [[UIView alloc] init];
    [bottomView addSubview:redView];
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(workLabel.mas_left).offset(-5);
        make.centerY.equalTo(rightLabel.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    redView.backgroundColor = [UIColor redColor];
}
- (void)testAction:(UIButton *)btn{
    if (btn.tag == 100) {
        
        ByRecordListVC *vc = [[ByRecordListVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
    }else if (btn.tag == 200){
        
        ByAccountDetailVC *vc = [[ByAccountDetailVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
   
  
}

- (void)enterNestWithNetModel:(NSString *)timeString{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.uid = BY_USER.uid;
    netModel.mobile = BY_USER.mobile;
    netModel.daytime = timeString;
    
    if (BY_USER.isWorker.integerValue == 1) {
        
        [ByApiRecord queryWorker:netModel success:^(ByNetBaseResult *result){
            
          [ByCoreToolAlert hideHud];
           NSArray *dataArray = result.data;
            if (dataArray.count == 0) {
                ByRecordVC *vc = [[ByRecordVC alloc] init];
                vc.isWorker = YES;
                [self by_normalPush:vc hasNavi:YES];
            }else{
               
                ByRecordListVC *vc = [[ByRecordListVC alloc] init];
                vc.daytime = timeString;
                [self by_normalPush:vc hasNavi:YES];
                
            }
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }else{
        
        [ByApiRecord queryOverseer:netModel success:^(ByNetBaseResult *result) {
            
            [ByCoreToolAlert hideHud];
            NSArray *dataArray = result.data;
            if (dataArray.count == 0) {
                ByRecordVC *vc = [[ByRecordVC alloc] init];
                vc.isWorker = NO;
                [self by_normalPush:vc hasNavi:YES];
                
            }else{
                
                ByRecordListVC *vc = [[ByRecordListVC alloc] init];
                vc.daytime = timeString;
                [self by_normalPush:vc hasNavi:YES];
            }
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }
}



- (void)selectAction:(UIButton *)btn{
    NSLog(@"选择月份");
    [self.monthDateView show];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
